/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;


import fetcher.Fetcher;
import NetListener.NetListener;
import NetListener.NetListenerElaboratedData;
import jWASA.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 *
 * @author gianmariospacagna
 */
public class Controller {

    Collection<Sample> samples;
    Collection<PHR> patientRecords;
    NetListener netListener;
    NetListenerElaboratedData elaboratedListener;
    Fetcher fetcher;
    /*Analyser analyser;
    ElaboratedSamples elaboratedSamples;*/

    public Controller() {
        //samples = Collections.synchronizedCollection(new ArrayList<Sample>());
        //patientRecords = Collections.synchronizedCollection(new ArrayList<PHR>());
        
        samples= new CopyOnWriteArrayList<Sample>();
        patientRecords= new CopyOnWriteArrayList<PHR>();
        fetcher = new Fetcher(samples, patientRecords);
        /*analyser= new Analyser();
        elaboratedSamples = new ElaboratedSamples();*/
    }

    public void startServer(String port) throws IOException {
        startServer(Integer.parseInt(port));
    }

    public void startServer(int port) throws IOException {

        if (netListener == null ) {
            netListener = new NetListener(port, samples, patientRecords);
            netListener.start();
        } else if (netListener.isAlive()){
            resumeServer();
        }
        else {
            netListener.start();
        }
    }

    public void stopServer() throws IOException {
        netListener.safeStop();
    }

    public void resumeServer() {
        netListener.safeResume();
    }

    public void startFetcher() {

        if (fetcher.isAlive()) {
            resumeFetcher();
        } else {
            fetcher.start();
        }
    }

    public void stopFetcher() {
        fetcher.safeStop();
    }

    public void resumeFetcher() {
        fetcher.safeResume();
        
    }
    public void startElaboratedDataNetListener(int port) throws IOException{
        
        //elaboratedListener=new NetListenerElaboratedData(port, elaboratedSamples);
    }
    public void startSimulation(){
        //analyser.analyseData(elaboratedSamples, patientRecords);
    }
}

